/*
Joseph Ratway
Garrett Brown
Bret Parker
*/

//imports
import java.util.*;


public class ReversiGame
{
    Board currentBoard = new Board();
    List<Board> boardStates = new ArrayList<Board>();
    Parser Parse = new Parser();
    Scanner user_input = new Scanner( System.in);
    int endgame;
	int numHumanPlayers;
	int difficulty;
	int startingColor;
	int confirm;

        
    ReversiGame()
	{
        setupGame();
    };
        
		
    void setupGame()
	{
        currentBoard.endCondition=2;
        currentBoard.numMoves=0;
		currentBoard.turnCount = 0;
		currentBoard.player = 1;
		initBoardArray();
		setInitialValues();
		currentBoard.printBoard();
		setPlayers();
	}
		
          
    void initBoardArray()
	{
        for (int i=0; i < 8; i++) 
        {
            for (int j=0; j < 8; j++) 
            {
                currentBoard.currentBoard[i][j]=new BoardSpace();
                currentBoard.currentBoard[i][j].locationValue=0;
            }
        }
	}
	
	void setInitialValues()
	{
		currentBoard.currentBoard[3][3].locationValue=2;
		currentBoard.currentBoard[3][4].locationValue=1;
		currentBoard.currentBoard[4][3].locationValue=1;
		currentBoard.currentBoard[4][4].locationValue=2;
	}
 
    void startGame()
    {
		if(currentBoard.endCondition<=0)
		{
			System.out.println("         GAME OVER");
			getScores("");
			System.out.println("DO YOU WANT TO PLAY AGAIN?(1=yes 2=no)");
			endgame = user_input.nextInt();
				
			if(endgame==1)
			{	
				setupGame();
				startGame();
			}
			
			else if(endgame==2)
			{
				return;
			}
			else
				System.out.println("Not a valid input");
				startGame();
		}
		else
		{
			Board pushBack = new Board();
			pushBack.deepCopy(currentBoard);
			boardStates.add(pushBack);
			
			if(numHumanPlayers==2)
			{
				Parse.ParseLine(currentBoard);
				currentBoard.printBoard();
				confirmMove();
				startGame();
			}		
			else if(numHumanPlayers==1)
			{
				if(difficulty==0)//novice
				{
						if(startingColor==2)
						{
							Parse.ParseLine(currentBoard);
							confirmMove();
							currentBoard.printBoard();
							noviceAI();
							confirmMove();
							startGame();
						}
						else if(startingColor==1)
						{
							noviceAI();
							confirmMove();
							currentBoard.printBoard();
							Parse.ParseLine(currentBoard);
							confirmMove();
							startGame();
						}
				}
				
				else if(difficulty==1)//easy
				{
						if(startingColor==2)
						{
						
							System.out.println("Selcting Easy");
							Parse.ParseLine(currentBoard);
							confirmMove();
							currentBoard.printBoard();
							easyAI();
							confirmMove();
							startGame();
						}
						else if(startingColor==1)
						{
							System.out.println("Selcting Easy");
							easyAI();
							confirmMove();
							currentBoard.printBoard();
							Parse.ParseLine(currentBoard);
							confirmMove();
							startGame();
						}
				}
				else if(difficulty==2)//medium
				{
						if(startingColor==2)
						{
							Parse.ParseLine(currentBoard);
							confirmMove();
							currentBoard.printBoard();
							noviceAI();
							confirmMove();
							startGame();
						}
						else if(startingColor==1)
						{
							noviceAI();
							confirmMove();
							currentBoard.printBoard();
							Parse.ParseLine(currentBoard);
							confirmMove();
							startGame();
						}
				}
				else if(difficulty==3) //expert
				{
						if(startingColor==2)
						{
							Parse.ParseLine(currentBoard);
							confirmMove();
							currentBoard.printBoard();
							noviceAI();
							confirmMove();
							startGame();
						}
						else if(startingColor==1)
						{
							noviceAI();
							confirmMove();
							currentBoard.printBoard();
							Parse.ParseLine(currentBoard);
							confirmMove();
							startGame();
						}
				}
			}
			else if(numHumanPlayers==0)
			{
				if(difficulty==0)
				{
					System.out.println("Selcting Novice");
					noviceAI();
					currentBoard.printBoard();
					confirmMove();
					startGame();
				}
				else if(difficulty==1)
				{
					System.out.println("Selcting Easy");
					easyAI();
					currentBoard.printBoard();
					confirmMove();
					startGame();
				}
				else if(difficulty==2)
				{
					System.out.println("Selcting Novice");
					noviceAI();
					currentBoard.printBoard();
					confirmMove();
					startGame();
				}
				else if(difficulty==3)
				{
					System.out.println("Selcting Novice");
					noviceAI();
					currentBoard.printBoard();
					confirmMove();
					startGame();
				}
			}
		}
    }
	
	void setPlayers()
	{
		int choosePlayers;
		System.out.println("How many human players? (0 = AI v AI,  1 = human v AI, 2 = human v human)" );
		choosePlayers = user_input.nextInt();
		
		if(choosePlayers==2)
		{
			numHumanPlayers=2; //Human vs Human
			//startGame();
			
			return;
		}
		else if(choosePlayers==1)	//AI vs Human
		{
			numHumanPlayers=1;
			setDifficulty();
			setStartingColor();
			return;
			//startGame();
		}
		else if(choosePlayers==0)	//AI vs AI; need to think of way to set two AIs
		{
			numHumanPlayers=0;
			setDifficulty();
			return;
		}
		else
		{
			System.out.println("Not a valid input");
			setPlayers();
		}
	}
	
	void setDifficulty()
	{
		int choiceDiff;
		System.out.println("Choose AI difficulty: (0 = Novice, 1 = Easy,  2 = Medium, 3 = Hard)");
		choiceDiff = user_input.nextInt();
		
		if(choiceDiff==2)
		{
			difficulty=2;
			return;
		}
		else if(choiceDiff==1)
		{
			difficulty=1;
			return;
		}
		else if(choiceDiff==0)
		{
			difficulty=0;
			return;
		}
		else
		{
			System.out.println("Not a valid input");
			setDifficulty();
		}
	}
	
	void setStartingColor()
	{
		int choosecolor;
		System.out.println("Choose your Color: (1=White, 2=Black)");
		choosecolor = user_input.nextInt();
		
		if(choosecolor==2)
		{
			startingColor=2;
			return;
		}
		else if(choosecolor==1)
		{
			startingColor=1;
			return;
		}
		
		else
		{
			System.out.println("Not a valid input");
			setStartingColor();
		}
	}
	
	void noviceAI()//AI should choose randomly
	{
		currentBoard.random(currentBoard.currentBoard);
	}
	
	void easyAI()
	{
		currentBoard.easy(currentBoard.currentBoard);
	
	}
	
	void confirmMove()
	{
		System.out.println("Do you want to confirm this move?(1 yes 2 no)" );
		confirm = user_input.nextInt();
		if(confirm==1)
		{
			currentBoard.turnCount++;
			currentBoard.printBoard();
			
		}
		else if(confirm==2)
		{
			int temp = boardStates.size() - 1;
			System.out.println("UNDO");
			for(int i = 0; i < boardStates.size(); i++)
			{
				boardStates.get(i).printBoard();
				System.out.println(boardStates.get(i).endCondition);
			}
			currentBoard = boardStates.get(temp);
			currentBoard.printBoard();
		
		}
		else
			System.out.println("Not a valid input");
	}
	
	
	
    void getScores(String str)
    {
        currentBoard.getScores(str);
    }
    
    
}